package com.Lyoto.NetNotify.Schedule;

import com.Lyoto.NetNotify.Utils.DateUtil;
import com.Lyoto.NetNotify.Utils.YmlUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Lyoto
 * @Description
 * @Date 2022-09-30 10:24
 * @Version
 **/
@Slf4j
public class JdkTimerTask implements TimerSchedule<Timer>{
    private static final AtomicInteger nameId = new AtomicInteger();
    private Timer timer;
    private String execTime = YmlUtil.prase("execTime");
    private Date execTimeDate;
    public static final String taskId = "jdkTimerTask-" + (nameId.incrementAndGet());

    public JdkTimerTask() {
        this.timer = new Timer(taskId, true);
        this.execTimeDate = DateUtil.convertFirstTime(new Date(), execTime);
        log.info("定时任务设置|设置首次执行时间 {}", DateUtil.dateDefaultFormat(execTimeDate));
    }


    @Override
    public Timer getTimer() {
        return timer;
    }

    @Override
    public void exec(Runnable command) {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("定时任务|任务开始时间:{} in->{}",DateUtil.dateDefaultFormat(new Date()),taskId);
                command.run();
                log.info("定时任务|end");
            }
        },execTimeDate,1000*60*60*24);
        log.info("初始化设置定时任务|set task at->{},in->{}",DateUtil.dateDefaultFormat(execTimeDate),taskId);
    }
    
    
}
